package com.fitzpatrickit.robotics.hexaped;

import lejos.robotics.subsumption.Behavior;

public class WalkForwardBehaviour implements Behavior {
	
	private volatile boolean suppressed = true;
	
	private final HexapedMoveController hexapedMoveController;
	private final SonarSampler sonarSampler;
	
	public WalkForwardBehaviour(HexapedMoveController moveController, SonarSampler sonarSampler) {
		this.hexapedMoveController = moveController;
		this.sonarSampler = sonarSampler;
	}

	@Override
	public boolean takeControl() {
		return !sonarSampler.getObjectDetected();
	}

	@Override
	public void action() {
		this.suppressed = false;
		
		hexapedMoveController.forward();
		
		while (!this.suppressed && !sonarSampler.getObjectDetected()) {
			Thread.yield();
		}
		
		hexapedMoveController.stop();
	}

	@Override
	public void suppress() {
		this.suppressed = true;
	}

}
